const { init } = require('./../../wxCloudClientSDK.umd.js')
// 指定云开发环境 ID
wx.cloud.init({
  env: "cloud1-9gel6ndh8e555155", // 当前的云开发环境 ID
});
    
const client = init(wx.cloud);

Page({
  data: {
    searchValue: '',
    categories: [
      { id: 1, name: '商品精选' },
      { id: 2, name: '种植产品' },
      { id: 3, name: '儿童齿科' }
    ],
    currentCategory: 1,
    recommendGoods: [{
      id: 1,
      name: '武汉尚小镜内镜诊疗医院',
      title: '家庭卡',
      price: 96,
      image: 'images/home_card.jpg'
    }],
    products: [
      {
        id: 1,
        name: '【种植集采价】韩国奥齿泰牙齿种植套餐（全包）',
        price: 3980,
        image: '/static/images/product1.png'
      },
      {
        id: 2,
        name: '【儿童树脂补牙】儿童树脂补牙/儿童补牙（限首颗）',
        price: 124,
        originalPrice: 300,
        image: '/static/images/product2.png'
      },
      {
        id: 3,
        name: '【儿童涂氟】全口涂氟套餐/呵护儿牙',
        price: 51,
        originalPrice: 200,
        image: '/static/images/product3.png'
      },
      {
        id: 4,
        name: '【早期矫正】儿童MRC早期肌功能矫治（简单病例）',
        price: 5985,
        image: '/static/images/product4.png'
      }
    ],
    // 当前选中的分类ID
    activeCategoryId: 1,
    // 商品分类数据
    categories1: [/*
      { id: 1, name: '热门推荐' },
      { id: 2, name: '手机数码' },
      { id: 3, name: '家用电器' },
      { id: 4, name: '食品饮料' },
      { id: 5, name: '服饰鞋帽' },*/
    ],
    // 商品列表数据
    products1: [
      /*{ id: 1, name: '商品1', price: 99.9, image: 'cloud://cloud1-9gel6ndh8e555155.636c-cloud1-9gel6ndh8e555155-1345681178/credits_1.jpg' },
      { id: 2, name: '商品2', price: 199.9, image: 'cloud://cloud1-9gel6ndh8e555155.636c-cloud1-9gel6ndh8e555155-1345681178/credits_1.jpg' },
      { id: 3, name: '商品3', price: 299.9, image: 'cloud://cloud1-9gel6ndh8e555155.636c-cloud1-9gel6ndh8e555155-1345681178/credits_1.jpg' },
      */
    ],
  },

  onLoad() {
    this.queryCategoryInfo();
    
  },

  queryCategoryInfo() {
    client.models.category_info.list({
      filter: {
        where: {
         $and: [{
           parentId: {
             $eq: 0
           },
         },
       ],
        },
        order: ['sortOrder ASC']
      },
      pageSize: 10,
      pageNumber: 1,
      getCount: true
    }).then(res => {
      
     console.log("category_info: " + res.data.records);
     
     this.parseCategoryData(res.data.records);

    

     if (res.data.records.length > 0) {
      const categoryId = res.data.records[0].categoryId;
      this.loadProducts(categoryId);
      this.setData({
        activeCategoryId: categoryId
      });
    }
   }).catch(res => {
     console.log("category_info err: " + res)
   });
  },

  parseCategoryData(records) {
    const parsedData = records.map(item => {
      return {
        id: item.categoryId,
        categoryId: item.categoryId,
        name: item.categoryName,
      };
    });

    this.setData({
      categories1: parsedData
    });
  },

  onSearch(e) {
    this.setData({
      searchValue: e.detail.value
    })
  },

  onCategoryChange(e) {
    const id = e.currentTarget.dataset.id
    this.setData({
      currentCategory: id
    })
    // TODO: 根据分类加载商品
  },

  goToProductDetail(e) {
    const id = e.currentTarget.dataset.id;
    console.log("id: " + id);
    wx.navigateTo({
      url: '/page/productdetail/index?param1=' + id,
    })
  },

  // 点击分类事件
  onCategoryTap(e) {
    const categoryId = e.currentTarget.dataset.id;

    console.log("categoryId: " + categoryId);

    this.setData({
      activeCategoryId: categoryId,
    });
    // 这里可以根据 categoryId 请求对应的商品列表
    this.loadProducts(categoryId);
  },

  // 模拟加载商品列表
  loadProducts(categoryId) {
    
      client.models.product_info.list({
        filter: {
          where: {
          $and: [{
            categoryId: {
              $eq: categoryId
            },
          },
        ],
          },
          order: ['categoryId ASC']
        },
        pageSize: 10,
        pageNumber: 1,
        getCount: true
      }).then(res => {
      console.log("product_info: " + res.data.records);
      
      this.parseProductData(res.data.records);
    }).catch(res => {
      console.log("product_info err: " + res)
    });
  },

  parseProductData(data) {
    const parsedData = data.map(item => {
      return {
        id: item._id,
        productId: item.productId,
        name: item.productName,
        price: item.price,
        image: item.images.url,
      };
    });

    this.setData({
      products1: parsedData
    });
  },
});